﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

//引用启业库添加日志
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Database;
using Microsoft.Practices.Unity;

public partial class Manage_login : System.Web.UI.Page
{
    BLL.UserBll ub = new BLL.UserBll();
    BLL.LogBll lb = new BLL.LogBll();

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        string name = user_name.Text.ToString();
        string password = user_password.Text.ToString();

        if (ub.Exists(name))
        {
            if (ub.GetPassword(name) == BLL.Cryptography.MD5(password))
            {
                //IUnityContainer container = new UnityContainer();
                //container.RegisterType<>();
                
                //添加登录日志
                //LogEntry logEntry = new LogEntry();
                /*
                logEntry.EventId = 1;
                logEntry.Priority = 1;
                logEntry.Title = "登录";
                logEntry.Message = "登录ID为"+name+"的用户登录到后台管理";
                logEntry.Categories.Add("Enterprise Library 5.0");
                logEntry.Categories.Add(".NET Framework");
                //写入日志
                //Logger.Writer.Write(logEntry, "General");
                 
                //ILogFormatter logFormatter = new TextFormatter("/");

                ////2.创建服务
                //ILog logServer = new TextFileLog(@"C:\test.log", logFormatter);

                ////3.使用服务
                //logServer.Write("日志正文");
                */

                if (ub.GetSateByUser(name) == 1)
                {
                    WriteLog(name);

                    FormsAuthentication.SetAuthCookie(name, false);
                    Response.Redirect(FormsAuthentication.DefaultUrl);
                }
                else
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "s", "alert('帐户无法登录，请与管理员联系');window.location.href=window.location.href;", true);
                }
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "s", "alert('密码输入错误');window.location.href=window.location.href;", true);
            }
        }
        else
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "s", "alert('此用户不存在');window.location.href=window.location.href;", true);
        }
    }

    private void WriteLog(string name)
    {
        LogEntry log = new LogEntry();
        Model.Log logEntity = new Model.Log();
      
        //log.Severity = System.Diagnostics.TraceEventType.Warning;
        logEntity.Win32ThreadId = log.Win32ThreadId;
        logEntity.Title = log.Title;
        logEntity.Timestamp = log.TimeStamp;
        logEntity.Severity = log.Severity.ToString();
        logEntity.ProcessName = log.ProcessName;
        logEntity.ProcessID = log.ProcessId;
        logEntity.Priority = log.Priority;
        logEntity.Message = "用户ID为"+name+"登录后台管理";
        logEntity.MachineName = log.MachineName;
        logEntity.EventID = log.EventId;
        logEntity.AppDomainName = log.AppDomainName;
        lb.WriteLog(logEntity);
    }

}
